using System;
using System.Collections.Generic;
using System.Reflection;
using UnityEditor;
using UnityEngine;
using UnityGameFramework.Editor;

namespace StarForce
{
    [CustomEditor(typeof(HotfixUGuiForm), true)]
    public class HotfixUGuiFormEditor : GameFrameworkInspector
    {
        private const string NoneOptionName = "<None>";

        private SerializedProperty m_HotfixUGuiFormName = null;
        
        private SerializedProperty m_UIOpen = null;
        private SerializedProperty m_UIClose = null;

        private string[] m_HotfixUGuiFormTypeNames = null;

        private int m_HotfixUGuiFormTypeNameIndex = 0;

        protected void OnEnable()
        {
            m_HotfixUGuiFormName = serializedObject.FindProperty("m_HotfixUGuiFormName");
            m_UIOpen = serializedObject.FindProperty("m_UIOpen");
            m_UIClose = serializedObject.FindProperty("m_UIClose");
            RefreshTypeNames();
        }

        private void RefreshTypeNames()
        {
            RenderHotfixName();
            serializedObject.ApplyModifiedProperties();
        }

        private void RenderHotfixName()
        {
            List<string> hotfitUGuiTypeNames = new List<string>
            {
                NoneOptionName
            };

            List<string> typeNames = new List<string>();
            Type typeBase = typeof(StarForce.Hotfix.HotfixUGuiForm);
            Assembly assembly = null;
            try
            {
                assembly = Assembly.Load("StarForce.Hotfix");
            }
            catch
            {
                return;
            }

            System.Type[] types = assembly.GetTypes();
            foreach (System.Type type in types)
            {
                if (type.IsClass && !type.IsAbstract && typeBase.IsAssignableFrom(type))
                {
                    typeNames.Add(type.Name);
                }
            }

            hotfitUGuiTypeNames.AddRange(typeNames);
            m_HotfixUGuiFormTypeNames = hotfitUGuiTypeNames.ToArray();
            m_HotfixUGuiFormTypeNameIndex = 0;
            if (!string.IsNullOrEmpty(m_HotfixUGuiFormName.stringValue))
            {
                m_HotfixUGuiFormTypeNameIndex = hotfitUGuiTypeNames.IndexOf(m_HotfixUGuiFormName.stringValue);
                if (m_HotfixUGuiFormTypeNameIndex <= 0)
                {
                    m_HotfixUGuiFormTypeNameIndex = 0;
                    m_HotfixUGuiFormName.stringValue = null;
                }
            }
        }

        public override void OnInspectorGUI()
        {
            base.OnInspectorGUI();

            serializedObject.Update();

            int hotfixFGuiFormSelectedType = EditorGUILayout.Popup("Hotfix FGui Form", m_HotfixUGuiFormTypeNameIndex,
                m_HotfixUGuiFormTypeNames);
            if (hotfixFGuiFormSelectedType != m_HotfixUGuiFormTypeNameIndex)
            {
                m_HotfixUGuiFormTypeNameIndex = hotfixFGuiFormSelectedType;
                m_HotfixUGuiFormName.stringValue = (hotfixFGuiFormSelectedType <= 0
                    ? null
                    : m_HotfixUGuiFormTypeNames[hotfixFGuiFormSelectedType]);
            }

            EditorGUILayout.PropertyField(m_UIOpen);
            EditorGUILayout.PropertyField(m_UIClose);
            serializedObject.ApplyModifiedProperties();
        }


        protected override void OnCompileComplete()
        {
            base.OnCompileComplete();

            RefreshTypeNames();
        }
    }
}